package com.gitee.shop.controller;

import com.gitee.shop.domain.Item;
import com.gitee.shop.domain.ItemCat;
import com.gitee.shop.service.ItemCatService;
import com.gitee.shop.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class IndexController {

    //注入service
    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemCatService itemCatService;

    @RequestMapping("/")
    public String index(Model model){
        List<Item> hotItems = itemService.selectByIflag(1);
        List<Item> prItems = itemService.selectByIflag(2);
        model.addAttribute("hotItems", hotItems);
        model.addAttribute("preItems", prItems);
        return "index";
    }

    //ajax 异步请求 加载分类信息
    @RequestMapping("/itemCat/list")
    @ResponseBody
    public List<ItemCat> list(){
        List<ItemCat> itemCats = itemCatService.selectByStatus(1);
        return itemCats;
    }

    //统一显示页面
    @RequestMapping("/{page}")
    public String page(@PathVariable String page){
        return page;
    }

}
